package com.incar.chuxing.utils;

import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.Gravity;
import android.widget.Toast;

import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.BceCredentials;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.auth.DefaultBceSessionCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.CreateBucketResponse;
import com.baidubce.services.bos.model.PutObjectResponse;
import com.incar.chuxing.baiduMap.GeoCoderUtils;
import com.incar.chuxing.utils.Toast.ToastCompat;

import java.io.File;
import java.io.IOException;

import static com.taobao.accs.ACCSManager.mContext;

/**
 * Created by Michael on 11/10.
 */

public class UploadPicUtil {

    private static UploadPicUtil instance;
    public OnFinishListener onFinishListener;

    public static UploadPicUtil getInstance() {
        if (instance == null) {
            instance = new UploadPicUtil();
        }
        return instance;
    }

    public UploadPicUtil() {
    }

    public void uploadPicToBos(final File file, String ak, String sk,
                               String endPoint, String stsToken, final String bucketName,
                               final String objectKey, final OnFinishListener onFinishListener) {
        // 1. get pic params from ui: file name, file location uri etc
        // 2. send params to app server and get sts, bucket name and region
        // 3. upload selected pic to bos with sts etc, which bos client needs

        this.onFinishListener = onFinishListener;

        if (file == null) {
            ToastCompat.makeText(mContext, "上传失败", Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
            return;
        }
        BceCredentials credentials = null;
        if (stsToken != null && !stsToken.equalsIgnoreCase("")) {
            credentials = new DefaultBceSessionCredentials(ak, sk, stsToken);
        } else {
            credentials = new DefaultBceCredentials(ak, sk);
        }

        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(credentials);
        //  config.setEndpoint(endPoint); //Bucket所在区域
        final BosClient client = new BosClient(config);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //上传Object
                    PutObjectResponse putObjectFromFileResponse = client.putObject(bucketName, getFileNameNoEx(file), file);
                    System.out.println(putObjectFromFileResponse.getETag());

                } catch (BceServiceException e) {
                    System.out.println("Error ErrorCode: " + e.getErrorCode());
                    System.out.println("Error RequestId: " + e.getRequestId());
                    System.out.println("Error StatusCode: " + e.getStatusCode());
                    System.out.println("Error Message: " + e.getMessage());
                    System.out.println("Error ErrorType: " + e.getErrorType());
                    // finished uploading file, send a message to inform ui
                    onFinishListener.onFailure(e.getErrorMessage());
                } catch (BceClientException e) {
                    System.out.println("Error Message: " + e.getMessage());
                }
                System.out.println("Error ErrorType: " + "上传成功");
                onFinishListener.onSuccess();
            }
        }).start();

    }


    public interface OnFinishListener {
        void onSuccess();

        void onFailure(String failReason);
    }

    /*
     * Java文件操作 获取不带扩展名的文件名
     */
    public String getFileNameNoEx(File nameFile) {
        String filename = nameFile.getName();
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }
}
